package com.interview.one.controller;

import com.interview.one.common.BusinessException;
import com.interview.one.common.CommonRes;
import com.interview.one.common.EmBusinessError;
import com.interview.one.domain.AddShipAddressReq;
import com.interview.one.domain.DeleteShipAddressReq;
import com.interview.one.domain.ModifyShipAddressReq;
import com.interview.one.model.ShipAddress;
import com.interview.one.service.ShipAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shipAddress")
public class ShipAddressController {
    public static final int IS_DEFAULT = 0;
    public static final int IS_NOT_DEFAULT = 1;

    @Autowired
    private ShipAddressService shipAddressService;

    @PostMapping("/getList")
    public CommonRes getList(@RequestHeader String token) throws BusinessException {
        List list = this.shipAddressService.getShipAddress(Integer.valueOf(token));
        if ((list == null) || (list.size() == 0)) {
            throw new BusinessException(EmBusinessError.NO_DATA_ERROR);
        }
        return CommonRes.create(list);
    }

    @PostMapping("/add")
    public CommonRes add(@RequestBody AddShipAddressReq req, @RequestHeader String token) {
        ShipAddress address = new ShipAddress();
        address.setShipAddress(req.getShipAddress());
        address.setShipUserMobile(req.getShipUserMobile());
        address.setShipUserName(req.getShipUserName());
        address.setUserId(Integer.valueOf(token));
        List list = this.shipAddressService.getShipAddress(Integer.valueOf(token));
        if ((list == null) || (list.size() == 0))
            address.setShipIsDefault(0);
        else {
            address.setShipIsDefault(1);
        }
        this.shipAddressService.addShipAddress(address);
        return CommonRes.create(null, "添加地址成功");
    }

    @PostMapping("/modify")
    public CommonRes modify(@RequestBody ModifyShipAddressReq req, @RequestHeader String token) {
        ShipAddress address = new ShipAddress();
        address.setId(req.getId());
        address.setShipAddress(req.getShipAddress());
        address.setShipUserMobile(req.getShipUserMobile());
        address.setShipUserName(req.getShipUserName());
        address.setShipIsDefault(req.getShipIsDefault());

        if (req.getShipIsDefault() == 0) {
            for (ShipAddress shipAddress : this.shipAddressService.getShipAddress(Integer.valueOf(token))) {
                shipAddress.setShipIsDefault(1);
                this.shipAddressService.modifyShipAddress(shipAddress);
            }

        }

        this.shipAddressService.modifyShipAddress(address);
        return CommonRes.create(null, "修改成功");
    }

    @PostMapping("/delete")
    public CommonRes delete(@RequestBody DeleteShipAddressReq req, @RequestHeader String token) {
        boolean isDefault = this.shipAddressService.getShipAddressById(req.getId()).getShipIsDefault() == 0;
        this.shipAddressService.deleteShipAddress(req.getId());
        List list = this.shipAddressService.getShipAddress(Integer.valueOf(token));
        if ((isDefault) && (list != null) && (list.size() > 0)) {
            ShipAddress address = (ShipAddress) list.get(0);
            address.setShipIsDefault(0);
            this.shipAddressService.modifyShipAddress(address);
        }
        return CommonRes.create(null, "删除成功");
    }
}
